#!/bin/bash
###
 # @Author: your name
 # @Date: 2021-09-28 16:09:48
 # @LastEditTime: 2021-10-15 21:57:16
 # @LastEditors: Please set LastEditors
 # @Description: In User Settings Edit
 # @FilePath: \smartCampus-admin\deploy.sh
### 
echo ""
# 输出当前时间
date --date='0 days ago' "+%Y-%m-%d %H:%M:%S"
echo "Start"
# 判断宝塔WebHook参数是否存在
if [ ! -n "$1" ];
then 
          echo "param参数错误"
          echo "End"
          exit
fi
# git项目路径
gitPath="/www/wwwroot/$1"
# git 网址
gitHttp="git@e.coding.net:guangdongyunhaidianzi/zhihuixiaoyuan/smartCampus.git"
#pm2项目名
pm2Name="smartCampus"
echo "Web站点路径：$gitPath"

#判断项目路径是否存在
if [ -d "$gitPath" ]; then
        cd $gitPath
        #判断是否存在git目录
        if [ ! -d ".git" ]; then
                echo "在该目录下克隆 git"
                sudo git clone $gitHttp gittemp
                sudo mv gittemp/.git .
                sudo rm -rf gittemp
        fi
        echo "拉取最新的项目文件"javascript:;
        sudo git reset --hard origin/master
        sudo git pull       
        #echo "设置目录权限"
        #sudo chown -R www:www $gitPath
        echo "拉取结束End"
        echo "安装node_MODULE"
        sudo cnpm i
        echo "打包构建 npm run build"
        sudo npm run build
        exit
else
        echo "该项目路径不存在"
                echo "新建项目目录"
        mkdir $gitPath
        cd $gitPath
        #判断是否存在git目录
        if [ ! -d ".git" ]; then
                echo "在该目录下克隆 git"
                sudo git clone $gitHttp gittemp
                sudo mv gittemp/.git .
                sudo rm -rf gittemp
        fi
        echo "拉取最新的项目文件"
        sudo git reset --hard origin/master
        sudo git pull
        echo "设置目录权限"
        sudo chown -R www:www $gitPath
        echo "End"
        exit
fi